/*
 *
 *      Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: lengleng (wangiegie@gmail.com)
 *
 */

package com.pig4cloud.pig.ai.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.ai.api.vo.CollectionVO;
import com.pig4cloud.pig.ai.api.vo.DataVO;
import com.pig4cloud.pig.ai.api.vo.KnowledgeVO;
import com.pig4cloud.pig.ai.service.knowZskService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.annotation.Inner;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
import java.io.InputStream;

/**
 * @author wyl
 * @date 2024/05/31
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/dataset")
@Tag(description = "dataset", name = "知识库模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class knowZskController {



	@Autowired
	private knowZskService knowZskService;
	
	/**
	 * 查询知识库列表
	 * @param page 参数集
	 */
	@Inner(false)
    @GetMapping("/page" )
	@Operation(summary = "分页查询" , description = "分页查询" )
	public R getfastList(@ParameterObject Page page,@ParameterObject KnowledgeVO knowledgeVO) throws IOException {
		//Page pa = new Page<KnowledgeVO>();
		//System.out.println(pa);
		return R.ok(knowZskService.fastList(page, knowledgeVO)); 
	}
	
	@Inner(false)
	@Operation(description = "知识库创建", summary = "知识库创建")
	@PutMapping("/insertCreate")
	public R insertCreate(@RequestBody KnowledgeVO knowledgeVO) {

        return R.ok(knowZskService.saveCreate(knowledgeVO));
    }
	
	//知识库详情
	@Inner(false)
	@Operation(description = "知识库详情", summary = "知识库详情")
	@GetMapping("/viewdetail/{id}")
	public R getByid(@PathVariable("id") String id) {

        return R.ok(knowZskService.getByid(id));
    }
	
	
	@Inner(false)
	@Operation(description = "知识库删除", summary = "知识库删除")
	@GetMapping("/delete/{id}")
	public R delete(@PathVariable("id") String id) {
		return R.ok(knowZskService.deleteByid(id));
	}
	
	
	/**
	 *  获取数据集列表
	 * @param page 参数集
	 */
	@Inner(false)
    @GetMapping("/dataSetList" )
	@Operation(summary = "分页查询" , description = "分页查询" )
	public R dataSetList(@ParameterObject Page page,@ParameterObject CollectionVO collectionVO) throws IOException {
		//Page pa = new Page<KnowledgeVO>();
		//System.out.println(pa);
		return R.ok(knowZskService.dataSetList(page, collectionVO)); 
	}
	
	//创建手动数据集
	@Inner(false)
	@Operation(description = "创建手动数据集", summary = "创建手动数据集")
	@PutMapping("/collectionCreate")
	public R collectionCreate(@RequestBody CollectionVO collectionVO) {

        return R.ok(knowZskService.collectionCreate(collectionVO));
    }
	
	
	//创建表格数据集
	@Inner(false)
	@Operation(description = "创建表格数据集", summary = "创建表格数据集")
	@PostMapping("/localFile")
	public R localFile(@RequestPart("file") MultipartFile file, @RequestParam String tmbId) throws IOException {
		
		return R.ok(knowZskService.filepushData(file,tmbId));
	}
	
	//创建文本数据集
	@Inner(false)
	@Operation(description = "创建本文数据集", summary = "创建文本数据集")
	@PostMapping("/localTextFile")
	public R localTextFile(@RequestPart("file") MultipartFile file, @RequestParam String tmbId) {
		
		String content = "";
        try {
           byte[] data = file.getBytes();
           content = new String(data, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        System.out.println(content);
		
        String originalFilename = file.getOriginalFilename();
        int dotIndex = originalFilename.indexOf(".");
        String result ="";
        if (dotIndex != -1) {
             result = originalFilename.substring(0, dotIndex);
        }
        
		CollectionVO vo = new CollectionVO();
		vo.setTmbId(tmbId);
		vo.setName(content);
		vo.setFileName(result);
		return R.ok(knowZskService.createText(vo));
	}
	
	
	/**
	 *  点击数据集获取列表
	 * @param page 参数集
	 */
	@Inner(false)
    @GetMapping("/datasetcoreList" )
	@Operation(summary = "分页查询" , description = "分页查询" )
	public R datasetcoreList(@ParameterObject Page page,@ParameterObject DataVO dataVO) throws IOException {
		return R.ok(knowZskService.datasetcoreList(page, dataVO)); 
	}
	
}
